<html>
<head><meta charset="utf-8"><title>atomic nand bug? · t-compiler/wg-llvm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/index.html">t-compiler/wg-llvm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/atomic.20nand.20bug.3F.html">atomic nand bug?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225977859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/atomic%20nand%20bug%3F/near/225977859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/atomic.20nand.20bug.3F.html#225977859">(Feb 11 2021 at 11:35)</a>:</h4>
<p>I was looking at how LLVM implements atomic nand to copy it to cg_clif. I noticed the following:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span>: <span class="kp">&amp;</span><span class="nc">AtomicUsize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="p">.</span><span class="n">fetch_nand</span><span class="p">(</span><span class="mi">42</span><span class="p">,</span><span class="w"> </span><span class="n">Ordering</span>::<span class="n">SeqCst</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite" data-code-language="GAS"><pre><span></span><code><span class="nl">playground:</span><span class="err">:</span><span class="nl">foo:</span>
    <span class="nf">movq</span>    <span class="p">(</span><span class="nv">%rdi</span><span class="p">),</span> <span class="nv">%rax</span>

<span class="nl">.LBB0_1:</span>
    <span class="nf">movl</span>    <span class="nv">%eax</span><span class="p">,</span> <span class="nv">%ecx</span>
    <span class="nf">notl</span>    <span class="nv">%ecx</span>
    <span class="nf">orq</span> <span class="no">$-43</span><span class="p">,</span> <span class="nv">%rcx</span>
    <span class="na">lock</span>        <span class="nf">cmpxchgq</span>    <span class="nv">%rcx</span><span class="p">,</span> <span class="p">(</span><span class="nv">%rdi</span><span class="p">)</span>
    <span class="nf">jne</span> <span class="no">.LBB0_1</span>
    <span class="nf">retq</span>
</code></pre></div>
<p>It first loads an 8 byte value and then uses two 4 byte operations which would set the most significant four bytes to zero. I don't think this is correct.</p>



<a name="225984349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/atomic%20nand%20bug%3F/near/225984349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/atomic.20nand.20bug.3F.html#225984349">(Feb 11 2021 at 12:45)</a>:</h4>
<p><code>orq $-43, %rcx</code> sets bits in upper part to 1</p>



<a name="225984455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/atomic%20nand%20bug%3F/near/225984455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/atomic.20nand.20bug.3F.html#225984455">(Feb 11 2021 at 12:46)</a>:</h4>
<p>But these bits shouldn't always be 1.</p>



<a name="225984501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/atomic%20nand%20bug%3F/near/225984501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/atomic.20nand.20bug.3F.html#225984501">(Feb 11 2021 at 12:47)</a>:</h4>
<p>For NAND and constant 42?</p>



<a name="225984548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/atomic%20nand%20bug%3F/near/225984548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/atomic.20nand.20bug.3F.html#225984548">(Feb 11 2021 at 12:47)</a>:</h4>
<p>Ah, I get it now.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>